<?php
/**
 * RedCross Application
 *
 * @copyright TODO: Add copyright notice
 */

/**
 * ShiftExchange gateway class for communicating with persistent storage
 */
class Core_Gateway_ShiftExchange extends RedCross_Gateway_GatewayAbstract
{
    /**
     * Constructor
     * Defines some private variables
     */
    public function __construct()
    {
        $this->_class = 'ShiftExchange';
        $this->_modelPath = 'Core_Model';
    }

    public function fetchAllForShift($shiftId) {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftExchange se')
            ->leftJoin('se.ShiftRole sr')
            ->where('sr.shift_id = ?', $shiftId);
        return $query->execute();
    }

    public function fetchAllUnhandledForShift($shiftId) {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftExchange se')
            ->leftJoin('se.ShiftRole sr')
            ->where('sr.shift_id = ?', $shiftId)
            ->andWhere('se.request_handled_time IS NULL');
        return $query->execute();
    }

    public function fetchAllHandledForShift($shiftId) {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftExchange se')
            ->leftJoin('se.ShiftRole sr')
            ->where('sr.shift_id = ?', $shiftId)
            ->andWhere('se.request_handled_time IS NOT NULL');
        return $query->execute();
    }

    public function fetchAllUnhandled() {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftExchange se')
            ->where('se.request_handled_time IS NULL');
        return $query->execute();
    }

    public function fetchById($shiftExchangeId) {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftExchange se')
            ->where('se.id = ?', $shiftExchangeId);
        return $query->fetchOne();
    }
}
